package com.all.play.myapplication.page;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.all.play.myapplication.R;
import com.all.play.myapplication.activity.MyViderPlayerActivity;
import com.all.play.myapplication.adapter.LocalVideoAdapter;
import com.all.play.myapplication.base.BasePager;
import com.all.play.myapplication.bean.MediaLocalVideoItem;
import com.all.play.myapplication.utils.LogUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/3/28.
 */

public class LocalVideoPager extends BasePager {


    private ListView lv_local_media;
    private TextView tv_nodata_show;
    private ProgressBar pb_loading;
    private LocalVideoAdapter videoAdapter;

    public LocalVideoPager(Context context) {
        super(context);
    }

    //建立集合.存储获取出的本地视频数据
    private List<MediaLocalVideoItem> medialist;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(medialist!=null&&medialist.size()>0){
                LogUtil.e(medialist.toString());
                //有数据
                //设置适配器
                videoAdapter = new LocalVideoAdapter(context,medialist);
                lv_local_media.setAdapter(videoAdapter);
                tv_nodata_show.setVisibility(View.GONE);
            }else {
                //没数据
                tv_nodata_show.setVisibility(View.VISIBLE);

            }

            //pb 隐藏
            pb_loading.setVisibility(View.GONE);

        }
    };

    @Override
    public View initView() {
        View view = View.inflate(context, R.layout.viewmedia_local, null);

        lv_local_media = view.findViewById(R.id.lv_local_media);
        tv_nodata_show = view.findViewById(R.id.tv_nodata_show);
        pb_loading = view.findViewById(R.id.pb_loading);

        //条目点击监听
        lv_local_media.setOnItemClickListener(new MyLvItemListener());
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        LogUtil.e("本地视频数据初始化");

        //加载本地数据
        getLocalMediaData();

    }

    /**
     *加载内存卡的数据,在子线程中执行
     *@author 聪明一只哈
     *@time 2018/3/29 10:42
     */
    private void getLocalMediaData() {
        new Thread(){
            @Override
            public void run() {
                super.run();

                //通过解析者获取数据
                ContentResolver resolver = context.getContentResolver();
                Uri uri= MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                String[] objs=new String[]{
                        MediaStore.Video.Media.DISPLAY_NAME,//文件的名字
                        MediaStore.Video.Media.DURATION,//文件的总时长
                        MediaStore.Video.Media.SIZE,//文件的大小
                        MediaStore.Video.Media.DATA,//文件的绝对地址
                        MediaStore.Video.Media.ARTIST//文件的艺术家

                };
                //查询
                Cursor cursor = resolver.query(uri, objs, null, null, null);
                //添加数据到集合中
                medialist=new ArrayList<MediaLocalVideoItem>();
                if(cursor!=null){
                    while (cursor.moveToNext()){

                        MediaLocalVideoItem mediaLocalVideoItem=new MediaLocalVideoItem();
                        medialist.add(mediaLocalVideoItem);

                        String name = cursor.getString(0);
                        mediaLocalVideoItem.setName(name);

                        long duration = cursor.getLong(1);
                        mediaLocalVideoItem.setDuration(duration);

                        long size = cursor.getLong(2);
                        mediaLocalVideoItem.setSize(size);

                        String data = cursor.getString(3);
                        mediaLocalVideoItem.setData(data);

                        String artist = cursor.getString(4);
                        mediaLocalVideoItem.setArtist(artist);
                    }
                }

                //发送消息
                handler.sendEmptyMessage(10);



            }
        }.start();

    }


    /**
     *条目点击监听事件
     *@author 聪明一只哈
     *@time 2018/3/29 13:34
     */
    class MyLvItemListener implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            MediaLocalVideoItem mediaLocalVideoItem = medialist.get(position);

            String data = mediaLocalVideoItem.getData();
            //调用系统所带的播放器
            /*Intent intent = new Intent();
            intent.setDataAndType(Uri.parse(data),"video*//*");
            context.startActivity(intent);*/

            //自己定义的播放器
            /*Intent intent = new Intent(context, MyViderPlayerActivity.class);
            intent.setDataAndType(Uri.parse(data),"video*//*");
            context.startActivity(intent);*/
            //将列表的数据传递过去
            Intent intent = new Intent(context, MyViderPlayerActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("videolist", (Serializable) medialist);
            intent.putExtras(bundle);
            intent.putExtra("position",position);
            context.startActivity(intent);



        }
    }


}
